package com.threeox.commonlibrary.entity.engine.request.sqlite;


import com.alibaba.fastjson.JSONObject;
import com.threeox.commonlibrary.entity.engine.request.base.BaseRequestMsg;
import com.threeox.commonlibrary.util.RegexHelper;
import com.threeox.commonlibrary.util.SqlUtil;
import com.threeox.commonlibrary.util.ValueUtils;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.res.StringHelper;

import java.util.List;

/**
 *
 * @ClassName: SqlitRequestMsg
 *
 * @Description: TODO(本地SQLIte数据库请求的对象)
 *
 * @author 赵屈犇
 *
 * @date 创建时间: 2017/7/4 12:39
 *
 * @version 1.0
 *
 */
public class SqliteRequestMsg extends BaseRequestMsg {

    public static final String LIMIT = "limit";// 每页多少条 请求Key值
    public static final String OFFSET = "offset";// 第几页 请求Key值
    public static final String DATABASEPATH = "DATABASEPATH";// 数据库本地地址  + System.currentTimeMillis()

    private String databasePath;// 数据库地址,默认可以不写,取本地创建的数据库
    private OperationTypeMsg operationType;// 操作的类型
    private String tableName; // 操作的表格名称
    private String executeSQL; //执行的SQL语句
    private JSONObject opertionParams; // 删除和修改需要的 操作的参数
    private List<String> requestParamName;// 查询的请求参数名称

    private JSONObject opertionParamsKeys;// 删除和修改需要的 操作 可替换的参数

    private String whereClause;
    private String[] whereArgs;

    private boolean isLimit = true;

    public void initData(StringHelper stringHelper) {
        this.tableName = stringHelper.getStringText(tableName);
        this.executeSQL = stringHelper.getStringText(executeSQL);
        this.executeSQL = SqlUtil.getSqlFrom(executeSQL);
        this.databasePath = stringHelper.getStringText(databasePath);
        RegexHelper.replaceSys(this.opertionParams);
        super.initData(stringHelper, EmptyUtils.isNotEmpty(tableName) ? tableName : executeSQL);
        this.opertionParamsKeys = RegexHelper.getReplaceKeys(opertionParams);
    }

    public String getDatabasePath() {
        return databasePath;
    }

    public void setDatabasePath(String databasePath) {
        this.databasePath = databasePath;
    }

    public OperationTypeMsg getOperationType() {
        return operationType;
    }

    public void setOperationType(OperationTypeMsg operationType) {
        this.operationType = operationType;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getExecuteSQL() {
        return executeSQL;
    }

    public void setExecuteSQL(String executeSQL) {
        this.executeSQL = executeSQL;
    }

    public List<String> getRequestParamName() {
        return requestParamName;
    }

    public void setRequestParamName(List<String> requestParamName) {
        this.requestParamName = requestParamName;
    }

    public JSONObject getOpertionParams() {
        return opertionParams;
    }

    public void setOpertionParams(JSONObject opertionParams) {
        this.opertionParams = opertionParams;
    }

    public String getWhereClause() {
        return whereClause;
    }

    public void setWhereClause(String whereClause) {
        this.whereClause = whereClause;
    }

    public String[] getWhereArgs() {
        return whereArgs;
    }

    public void setWhereArgs(String[] whereArgs) {
        this.whereArgs = whereArgs;
    }

    public boolean isLimit() {
        return isLimit;
    }

    public void setLimit(boolean limit) {
        isLimit = limit;
    }

    /**
    public void initOpertionParams(Intent intent) {
        RegexHelper.replace(this.opertionParams, this.opertionParamsKeys, intent);
    }*/

    public void initOpertionParams(Object data) {
        RegexHelper.replace(this.opertionParams, this.opertionParamsKeys, data);
    }

    /**
    @Override
    public void initRequestParam(Intent intent) {
        super.initRequestParam(intent);
        try {
            String dataBasePath = intent.getStringExtra(DATABASEPATH);
            if (EmptyUtils.isNotEmpty(dataBasePath)) {
                this.databasePath = dataBasePath;
            }
        } catch (Exception e) {
            LogUtils.e(TAG, "initRequestParam:报错了:" + e.getMessage());
        }
    }*/

    @Override
    public void initRequestParam(Object data) {
        super.initRequestParam(data);
        try {
            Object result = ValueUtils.getObjValue(data, DATABASEPATH);
            if (EmptyUtils.isNotEmpty(result)) {
                this.databasePath = String.valueOf(result);
            }
        } catch (Exception e) {
        }
    }
}
